//
//  UIImage+CAIGradient.m
//  CAICategories
//
//  Created by 海参 on 2022/8/30.
//  Copyright © 2022 com.Chs. All rights reserved.
//
#import "UIImage+CAIGradient.h"

@implementation UIImage (Gradient)


+ (UIImage *)zx_gradientImageWithSize:(CGSize)size
                               locations:(const CGFloat[_Nullable])locations
                              components:(const CGFloat[_Nullable])components
                                   count:(NSInteger)count
                           directionType:(ZXGradientImageDirection)directon
{
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //创建色彩空间
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //指定渐变色；colorSpace：颜色空间；components：颜色数组；locations：颜色所在位置（0～1）；count：渐变个数
    CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, count);
    CGColorSpaceRelease(colorSpace);
    CGPoint startPoint = (CGPoint){0,0};
    CGPoint endPoint = (CGPoint){size.width,0};
    if (directon == ZXGradientImageDirectionVertical)
    {
        startPoint = (CGPoint){0,0};
        endPoint = (CGPoint){0,size.height};
    }
    CGContextDrawLinearGradient(context, colorGradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
    CGGradientRelease(colorGradient);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



+ (UIImage *)zx_gradientHorizontalDoubleColorImageWithSize:(CGSize)size
                                                startColor:(UIColor *)startColor
                                                  endColor:(UIColor *)endColor
{
    CGFloat startRed, startGreen, startBlue,startAlpha;
    [startColor getRed:&startRed green:&startGreen blue:&startBlue alpha:&startAlpha];
    CGFloat endRed, endGreen, endBlue,endAlpha;
    [endColor getRed:&endRed green:&endGreen blue:&endBlue alpha:&endAlpha];
    
    const CGFloat location[] ={0,1};
    const CGFloat components[] ={
        startRed,startGreen,startBlue,startAlpha,
        endRed,endGreen,endBlue,endAlpha
    };
    UIImage *backgroundImage = [UIImage zx_gradientImageWithSize:size locations:location components:components count:2 directionType:ZXGradientImageDirectionHorizontal];
    return backgroundImage;
}


+ (UIImage *)zx_gradientVerticalDoubleColorImageWithSize:(CGSize)size
                                                topColor:(UIColor *)topColor
                                             bottomColor:(UIColor *)bottomColor
{
    CGFloat startRed, startGreen, startBlue,startAlpha;
    [topColor getRed:&startRed green:&startGreen blue:&startBlue alpha:&startAlpha];
    CGFloat endRed, endGreen, endBlue,endAlpha;
    [bottomColor getRed:&endRed green:&endGreen blue:&endBlue alpha:&endAlpha];
  
    const CGFloat location[] ={0,1};
    const CGFloat components[] ={
        startRed,startGreen,startBlue,startAlpha,
        endRed,endGreen,endBlue,endAlpha
    };
    UIImage *backgroundImage = [UIImage zx_gradientImageWithSize:size locations:location components:components count:2 directionType:ZXGradientImageDirectionVertical];
    return backgroundImage;
}

@end
